import os
import time

from selenium.webdriver.common.by import By

from common.base import Base
from common.function import open_browser
from selenium.webdriver.common.keys import Keys

photo_path=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'小新.jpg')
photo_path2=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'小新2.jpg')


class GoodManage(Base):

    def bk_username(self, username):  # 输入用户名
        self.send_keys((By.NAME, 'username'), username)

    def bk_password(self, password):  # 输入密码
        self.send_keys((By.NAME, 'password'), password)

    def login_bnt(self):  # 点击登录按钮
        self.click((By.CLASS_NAME, 'btn-a'))

    def add_goods(self):
        self.swtich_iframe((By.ID,'menu-frame'))#切换iframe才能点击1和2
        self.click((By.CLASS_NAME,'icon-goods'))#点击商品管理 1
        self.click((By.XPATH,'//li[@id="sub-menu-02_goods_add"]//a'))#点击添加新商品  2
        self.switch_to_default_content()
        self.swtich_iframe((By.ID,'main-frame'))#切换iframe
        goods_name=self.elelocate((By.XPATH,'//input[@name="goods_name"]')) #商品名称输入框
        goods_name.click()
        goods_name.send_keys('京城航少')
        goods_num=self.elelocate((By.XPATH,'//input[@name="goods_sn"]'))#商品货号输入框
        goods_num.click()
        goods_num.send_keys('1300247')
        self.select_option3((By.XPATH,"//select[@name='cat_id']"),'手机')#选择商品分类
        self.click((By.XPATH,'//input[@type="button"and@value="添加"]'))#扩展添加商品分类
        self.select_option3((By.XPATH,"//select[@name='other_cat[]']"),'数码时尚')
        #商品分类
        self.select_option((By.XPATH,"//select[@name='brand_id']"),3)
        self.select_option((By.ID,'suppliers_id'),1)#选择北京供货
        sale=self.elelocate((By.NAME,'shop_price'))#清空价格框之后重新输入价格
        sale.send_keys(Keys.CONTROL, 'a')
        time.sleep(2)
        sale.send_keys('200')
        # 重新输入会员价格一栏
        vip1=self.elelocate((By.ID,'rank_1'))
        vip1.clear()
        vip1.send_keys(2)
        vip2 = self.elelocate((By.ID, 'rank_3'))
        vip2.clear()
        vip2.send_keys(2)
        vip3 = self.elelocate((By.ID, 'rank_2'))
        vip3.clear()
        vip3.send_keys(2)
        #商品优惠价格
        self.send_keys((By.XPATH,"//input[@name='volume_number[]']"),1000)
        self.send_keys((By.XPATH,"//input[@name='volume_price[]']"),200)
        #市场售价
        market_sale=self.elelocate((By.NAME,'market_price'))
        market_sale.clear()
        market_sale.send_keys('280')
        #虚拟销量
        virtual_sale=self.elelocate((By.NAME,'virtual_sales'))
        virtual_sale.clear()
        virtual_sale.send_keys('25')
        #赠送消费积分
        give_score=self.elelocate((By.NAME,'give_integral'))
        give_score.clear()
        give_score.send_keys('3')
        #赠送等级分数
        give_class_score=self.elelocate((By.NAME,'rank_integral'))
        give_class_score.clear()
        give_class_score.send_keys('5')
        # 积分购买金额
        score_pay=self.elelocate((By.NAME,'integral'))
        score_pay.clear()
        score_pay.send_keys('300')
        # 促销价：
        self.click((By.ID,'is_promote'))
        promote_sale=self.elelocate((By.ID,'promote_1'))
        promote_sale.clear()
        promote_sale.send_keys('2999')
        #上传商品图片
        self.send_keys((By.XPATH,"//input[@name='goods_img']"),photo_path)
        self.send_keys((By.NAME,'goods_img_url'),'www.baidu.com')
        #上传缩略图
        self.send_keys((By.XPATH, "//input[@name='goods_thumbs']"), photo_path2)
        self.send_keys((By.XPATH, "//input[@value='商品缩略图外部URL']"), 'www.baid.com')
        #点击确定按钮
        self.click((By.XPATH,'//input[@value=" 确定 "]'))
        #返回上一页
        self.click((By.LINK_TEXT,'返回上一页'))

    def detail_describe(self):
        self.click((By.ID,'detail-tab'))#点击详细描述
        self.swtich_iframe((By.ID,'goods_desc___Frame'))
        self.swtich_iframe((By.XPATH,'//td[@id="xEditingArea"]//iframe'))#切换到body里的iframe
        textarea=self.elelocate((By.XPATH,'//body[@spellcheck="false"]'))
        textarea.click()
        time.sleep(2)
        textarea.send_keys('是个色小鬼,老喜欢调戏美女令居姐姐')
        time.sleep(2)
        self.switch_to_default_content()
        self.swtich_iframe((By.ID, 'main-frame'))
        self.click((By.XPATH,'//input[@type="button"and@value=" 确定 "]'))
        time.sleep(2)
        self.switch_alert()

    def other_message(self):
        self.click((By.ID,'mix-tab'))
        self.send_keys((By.NAME,'goods_weight'),'0.5KG')
        #商品库存数量
        goods_num=self.elelocate((By.XPATH,"//input[@name='goods_number']"))
        goods_num.clear()
        goods_num.send_keys('500')
        #警示数量
        alert_num=self.elelocate((By.NAME,'warn_number'))
        alert_num.clear()
        alert_num.send_keys('200')
        #多选框
        op1=self.click((By.NAME,'is_best'))
        op2=self.click((By.NAME,'is_new'))
        #点击是否免运费
        self.click((By.NAME,'is_shipping'))
        #商品关键词
        self.send_keys((By.NAME,'keywords'),'土豪金')
        #商品描述
        self.send_keys((By.NAME,'goods_brief'),'小新')
        #商家备注
        self.send_keys((By.NAME,'seller_note'),'超级便宜')
        #点击确定
        self.click((By.XPATH,'//div[@class="button-div"]//input[2]'))
        time.sleep(2)
        self.switch_alert()

    def goods_proper(self):
        self.click((By.ID,'properties-tab'))#点击商品属性
        #点击商品分类下拉框
        self.select_option3((By.XPATH,'//select[@name="goods_type"]'),'手机')













if __name__ == '__main__':
    driver=open_browser()
    go=GoodManage(driver)
    go.input_url('http://139.129.26.163/admin ')
    go.bk_username('admin')
    go.bk_password('admin123')
    go.login_bnt()
    go.add_goods()
    go.detail_describe()
    go.other_message()

















